package action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import dao.BookDAO;
import dao.LendRecordDAO;
import model.BookModel;
import model.LendRecordModel;
import model.UserModel;

import java.util.List;
import java.util.Map;

/**
 * 借书相关操作action类
 */
public class LendRecordAction extends BaseAction implements ModelDriven<BookModel> {
    private BookModel model = new BookModel();
    private String cback;
    private String resultCode;
    private String resultInfo;

    public String getCback() {
        return cback;
    }

    public void setCback(String cback) {
        this.cback = cback;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultInfo() {
        return resultInfo;
    }

    public void setResultInfo(String resultInfo) {
        this.resultInfo = resultInfo;
    }

    private List<LendRecordModel> list;

    public List<LendRecordModel> getList() {
        return list;
    }

    public void setList(List<LendRecordModel> list) {
        this.list = list;
    }

    /**
     * 借书操作
     * @return
     */
    public String add(){
        Map session = ActionContext.getContext().getSession();
        UserModel user = (UserModel)session.get("user");
        int bookId = this.getModel().getId();
        LendRecordDAO dao = new LendRecordDAO();
        if(dao.getLendRecordByUserIdAndBookId(user.getUserId(),bookId)!=null){
            this.resultCode="1111";
            this.resultInfo="对不起，您已经借过该书了！";
        }else{
            //将书籍的可借数减一
            BookDAO bd = new BookDAO();
            BookModel bm = bd.getBookById(bookId);
            LendRecordModel m = new LendRecordModel();
            m.setBookId(this.getModel().getId());
            m.setUserId(user.getUserId());
            if(dao.createLendRecord(m)){
                this.resultCode = "0000";
                this.resultInfo = "借书成功！";
            }else{
                this.resultCode = "0001";
                this.resultInfo = "借书失败！";
            }
        }
          return "rs";
    }

    /**
     * 还书操作
     * @return
     */
    public String back(){
        Map session = ActionContext.getContext().getSession();
        UserModel user = (UserModel)session.get("user");
        int bookId = this.getModel().getId();
        LendRecordDAO dao = new LendRecordDAO();
        if(dao.deleteLendRecord(user.getUserId(),bookId)){  //删除借书记录
            BookDAO d = new BookDAO();
            BookModel b = d.getBookById(bookId);
            d.updateBook(b);
            this.resultCode = "0000";
            this.resultInfo = "还书成功！";
        } else{
            this.resultCode = "0001";
            this.resultInfo = "还书失败！";
        }
        return "rs";
    }

    public String list(){
        Map session = ActionContext.getContext().getSession();
        UserModel user = (UserModel)session.get("user");
        LendRecordDAO dao = new LendRecordDAO();
        this.list = dao.findLendRecordByUserId(user.getUserId());
        return "list";
    }


    @Override
    public BookModel getModel() {
        return model;
    }

}
